﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Business;
using NetDon.OA.Interface;
using NetDon.OA.Interface.CRM;
using NetDon.Utilities;
using NetDon.WebControls;

namespace NetDon.OA.Portal.Shunting
{
    [Navigation("干线流程", "现场装车管理")]
    public partial class OnsiteEntruckingManagement : PageBase
    {
        protected int? CurrentOrderStatus { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnBatchEdit.Attributes.Add("onclick", "batchModifyOrder('{0}');return false;".Formato(this.gridOrderList.ClientID));
            if (!IsPostBack)
            {
                PageQueryAdapter1.PageSize = 20;
             
            
                this.ddlCustomer.InsertEmptyItem(ListEmptyType.Select);
                CurrentOrderStatus = (int) OrderStatus.Shunted;
             
                txtReceivingOrderDateStart.Text =
       txtPlannedShippingDateStart.Text =
       DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                txtReceivingOrderDateEnd.Text =
              txtPlannedShippingDateEnd.Text =
             DateTime.Now.ToString("yyyy-MM-dd");
                ddlCompany.InsertEmptyItem(ListEmptyType.Select);
                if (!this.IsSuperRoles())
                {
                    ddlCompany.SelectedValue = CurrentApplicationContext.CompanyId.ToString();
                    ddlCompany.Enabled = false;
                }
                PageQueryAdapter1.PerformQuery();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ObjectDataSourceCustomers.Selecting += (ds, dsArgs) =>
            {
                dsArgs.InputParameters["areaId"] = CurrentApplicationContext.CompanyId.ToString();
            };


        }

        public void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(ddlCustomer.SelectedValue))
            {

                LoadGoodsShippedTypes(ddlCustomer.SelectedValue.ToGuid());
            }


        }

        private void LoadGoodsShippedTypes(Guid customerId)
        {
            this.ddlGoodsShippedTypeId.Items.Clear();
            this.ddlGoodsShippedTypeId.DataSource = ServiceContainer.GetService<ICustomerInfoService>().GetCustomerGoodsShippTypes(
               customerId, CurrentApplicationContext.CompanyId);
            this.ddlGoodsShippedTypeId.DataBind();
            ddlGoodsShippedTypeId.InsertEmptyItem(ListEmptyType.Select);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }


        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {

            var service = ServiceContainer.GetService<IIssuingOrderService>();
            e.DataSource = service.SearchOrdersByCondition((IssuingOrderSearchCriteria)e.Condition, gridOrderList.SortExpression,
                                                      gridOrderList.SortDirection, e.PageSize, e.PageIndex);

        }

        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            var service = ServiceContainer.GetService<IIssuingOrderService>();
            e.RecordCount = service.GetOrderCount((IssuingOrderSearchCriteria)e.Condition);
        }

        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {


            var criteria = new IssuingOrderSearchCriteria
            {
              
            };

            if (!MiscUtilities.IsNullOrEmpty(ddlCompany.SelectedValue))
            {
                criteria.AreaId = ddlCompany.SelectedValue.ToGuid();
            }

            DateTime? dateStart = null, dateEnd = null;
            if (!string.IsNullOrEmpty(txtReceivingOrderDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtReceivingOrderDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtReceivingOrderDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtReceivingOrderDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.ReceivingOrderDate = new Range<DateTime?>(dateStart, dateEnd);
            }

            dateStart = null; dateEnd = null;
            if (!string.IsNullOrEmpty(txtPlannedShippingDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtPlannedShippingDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtPlannedShippingDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtPlannedShippingDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.PlannedShippingDate = new Range<DateTime?>(dateStart, dateEnd);
            }
            //dateStart = null; dateEnd = null;
            //if (!string.IsNullOrEmpty(txtActualShippingDateStart.Text.Trim()))
            //{
            //    dateStart = DateTime.Parse(txtActualShippingDateStart.Text.Trim());
            //}
            //if (!string.IsNullOrEmpty(txtActualShippingDateEnd.Text.Trim()))
            //{
            //    dateEnd = DateTime.Parse(txtActualShippingDateEnd.Text.Trim());
            //}
            //if (dateStart.HasValue || dateEnd.HasValue)
            //{
            //    criteria.ActualShippingDate = new Range<DateTime?>(dateStart, dateEnd);
            //}

            if (!string.IsNullOrEmpty(txtOrderCode.Text.Trim()))
            {
                criteria.OrderCode = txtOrderCode.Text.Trim();
            }
            if (!string.IsNullOrEmpty(ddlProvince.SelectedValue))
            {
                criteria.DeliveryProvince = ddlProvince.SelectedValue;
            }
            if (!string.IsNullOrEmpty(ddlCity.SelectedValue))
            {
                criteria.DeliveryCity = ddlCity.SelectedValue;
            }
            if (!string.IsNullOrEmpty(txtTransportationPlanCode.Text.Trim()))
            {
                criteria.TransportationPlanCode = txtTransportationPlanCode.Text.Trim();
            }
            if (!string.IsNullOrEmpty(txtOrderDeliveryCode.Text.Trim()))
            {
                criteria.DeliveryOrderCode = txtOrderDeliveryCode.Text.Trim();
            }
            //if (!string.IsNullOrEmpty(ddlPaymentMethod.SelectedValue))
            //{
            //    criteria.PaymentMethod = ddlPaymentMethod.SelectedValue.ToGuid();
            //}
            //if (!string.IsNullOrEmpty(ddlEntruckingAddress.SelectedValue))
            //{
            //    criteria.EntruckingAddress = ddlEntruckingAddress.SelectedValue.ToGuid();
            //}
            if (!string.IsNullOrEmpty(ddlCheckoutType.SelectedValue))
            {
                criteria.CheckoutType = (int)EnumUtilities.Parse<CheckoutType>(ddlCheckoutType.SelectedValue); ;
            }
            if (!string.IsNullOrEmpty(ddlRouteTypeId.SelectedValue))
            {
                criteria.RouteTypeId = ddlRouteTypeId.SelectedValue.ToGuid();
            }
            if (!string.IsNullOrEmpty(ddlGoodsShippedTypeId.SelectedValue))
            {
                criteria.GoodsShippedTypeId = ddlGoodsShippedTypeId.SelectedValue.ToGuid();
            }
            if (!string.IsNullOrEmpty(ddlCustomer.SelectedValue))
            {
                criteria.CustomerId = ddlCustomer.SelectedValue.ToGuid();
            }
            var context = (CurrentApplicationContext );
            if (context.BusinessType != BusinessType.All)
            {
                criteria.BusinessType = (int)context.BusinessType;
            }
          
            if (!string.IsNullOrEmpty(ddlOnsiteOperator.SelectedValue))
            {
                criteria.OnsiteOperator = ddlOnsiteOperator.SelectedValue.ToGuid();
            }
            if(ckbIsGoodsJitedYes.Checked ||ckbIsGoodsJitedNo.Checked)
            {
                criteria.IsGoodsJited = ckbIsGoodsJitedYes.Checked;
            }


            if (!string.IsNullOrWhiteSpace(ddlIssuingOrderStatus.SelectedValue))
            {
                criteria.OrderStatus = (int)ddlIssuingOrderStatus.SelectedValue.ToEnum<OrderStatus>();
            }

            //dateStart = null; dateEnd = null;
            //if (!string.IsNullOrEmpty(txtStartShippmentDateStart.Text.Trim()))
            //{
            //    dateStart = DateTime.Parse(txtStartShippmentDateStart.Text.Trim());
            //}
            //if (!string.IsNullOrEmpty(txtStartShippmentDateEnd.Text.Trim()))
            //{
            //    dateEnd = DateTime.Parse(txtStartShippmentDateEnd.Text.Trim());
            //}
            //if (dateStart.HasValue || dateEnd.HasValue)
            //{
            //    criteria.StartShippmentDate = new Range<DateTime?>(dateStart, dateEnd);
            //}
            e.Condition = criteria;
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.ReloadCurrentPage();
        }

        protected void gridOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (e.Row.DataItem);

                var orderId = e.Row.Attributes["Id"] = Convert.ToString(DataBinder.Eval(entity, "Id"));


                var btnEdit = (ImageButton)e.Row.FindControl("btnEdit");
                if (btnEdit != null)
                {
                    btnEdit.OnClientClick = string.Format("modifyOrder('{0}');return false;", orderId);
                }



                var btnDetails = (ImageButton)e.Row.FindControl("btnDetails");
                if (btnDetails != null)
                {
                    btnDetails.OnClientClick = string.Format("modifyOrder('{0}');return false;", orderId);
                }

                if (!MiscUtilities.IsNullOrEmpty(DataBinder.Eval(entity, "IsGoodsJilted")) && Convert.ToBoolean(DataBinder.Eval(entity, "IsGoodsJilted")))
                {
                    e.Row.Style.Add("background-color", "#FF6600");

                }
                if (!MiscUtilities.IsNullOrEmpty(DataBinder.Eval(entity, "DelayedDays")) && Convert.ToInt32(DataBinder.Eval(entity, "DelayedDays"))>1)
                {
                    e.Row.Style.Add("background-color", "#3366FF");

                }

            }
        }


    }
}